//
//  OrderReplyCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/29.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class OrderReplyCell: UITableViewCell {
  static var identifier = "OrderReplyCell.swordjoy"
  
  static func height(with model: Record) -> CGFloat {
    
    if model.images.isEmpty {
      
      return UIFont.calculateHeight(width: UIScreen.width - 125, font: GlobalConfig.system15Font, text: model.content) + 75
      
    } else {
      
      return UIFont.calculateHeight(width: UIScreen.width - 125, font: GlobalConfig.system15Font, text: model.content) + 65 + (UIScreen.width - 115) / 4
      
    }
    
  }
  
  var model: Record? {
    
    didSet {
      
      guard let temp = model else { return }
      
      headIconImageV.setImage(with: temp.headImage, placeholder: GlobalConfig.DefaultHeadIconImage)
      nickNameLabel.text = temp.staffName
      timeLabel.text = DateHelper.getFormatTime(timeInmillisecond: temp.createTime)
      commentLabel.text = temp.content
      
      if temp.images.isEmpty {
        
        collectionV.isHidden = true
      }
      else {
        
        collectionV.isHidden = false
        
        runInBackground {
          self.collectionV.reloadData()
        }
        
      }
      
    }
    
  }
  
  var cellDidSelect: ( (String) -> () )?
  
  fileprivate var contentV: UIView!
  
  fileprivate var headIconImageV: UIImageView!
  fileprivate var nickNameLabel: UILabel!
  fileprivate var timeLabel: UILabel!
  fileprivate var commentLabel: UILabel!
  fileprivate var collectionV: UICollectionView!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    backgroundColor = UIColor.clear
    
    selectionStyle = .none
    
    create()
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension OrderReplyCell {
  fileprivate func create() {
    contentV = UIView()
    contentView.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalToSuperview()
      make.bottom.equalTo(-5)
    }
    
    headIconImageV = UIImageView()
    headIconImageV.image = GlobalConfig.DefaultHeadIconImage
    contentV.addSubview(headIconImageV)
    headIconImageV.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalTo(15)
      make.height.width.equalTo(40)
    }
    
    nickNameLabel = UILabel()
    contentV.addSubview(nickNameLabel)
    nickNameLabel.font = GlobalConfig.system16Font
    nickNameLabel.textColor = GlobalConfig.TextDeepColor
    nickNameLabel.snp.makeConstraints { (make) in
      make.left.equalTo(headIconImageV.snp.right).offset(15)
      make.top.equalTo(15)
    }
    
    timeLabel = UILabel()
    contentV.addSubview(timeLabel)
    timeLabel.font = GlobalConfig.system12Font
    timeLabel.textColor = GlobalConfig.TextLightColor
    timeLabel.snp.makeConstraints { (make) in
      make.right.equalTo(-20)
      make.top.equalTo(15)
    }
    
    commentLabel = UILabel()
    contentV.addSubview(commentLabel)
    commentLabel.font = GlobalConfig.system15Font
    commentLabel.numberOfLines = 0
    commentLabel.textColor = GlobalConfig.Text08DeepColor
    commentLabel.snp.makeConstraints { (make) in
      make.left.equalTo(headIconImageV.snp.right).offset(15)
      make.right.equalTo(-40)
      make.top.equalTo(nickNameLabel.snp.bottom).offset(9)
    }
    
    let flow = UICollectionViewFlowLayout()
    let width = (UIScreen.width - 115) / 4
    flow.itemSize = CGSize(width: width, height: width)
    flow.minimumInteritemSpacing = 5
    
    collectionV = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
    collectionV.backgroundColor = UIColor.clear
    contentV.addSubview(collectionV)
    collectionV.dataSource = self
    collectionV.delegate = self
    collectionV.register(ImageSelectCell.self, forCellWithReuseIdentifier: ImageSelectCell.identifier)
    collectionV.register(ChooseImageCell.self, forCellWithReuseIdentifier: ChooseImageCell.identifier)
    collectionV.snp.makeConstraints { (make) in
      make.left.equalTo(75)
      make.right.equalTo(-5)
      make.top.equalTo(commentLabel.snp.bottom).offset(10)
      make.height.equalTo(width)
    }
  }
}

extension OrderReplyCell: UICollectionViewDataSource {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return model?.images.count ?? 0
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageSelectCell.identifier, for: indexPath) as! ImageSelectCell
    cell.isHideDeleteButton = true
    cell.imageName = model?.images[indexPath.row].thumbImageURL ?? ""
    
    return cell
  }
}

extension OrderReplyCell: UICollectionViewDelegate {
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    guard let closure = cellDidSelect else { return }
    closure(model?.images[indexPath.row].thumbImageURL ?? "")
    
  }
  
}
